<?php defined('IN_LK') or die('Access Denied');
/**
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		lk framework
 * @author		<yangzhiguo0903@163.com>
 * @copyright	Copyright (c) 2010 - 2011, yzg, Inc.
 * @license		http://www.apache.org/licenses/LICENSE-2.0
 * @link		http://www.amiaoagou.com
 * @since		Version 1.0.0
 * @filesource
 */

/**
 * 性能基准
 *
 * This class enables you to mark points and calculate the time difference
 * between them.  Memory consumption can also be displayed.
 *
 * @package		lk
 * @subpackage	lk
 * @category	core
 * @author		<yangzhiguo0903@163.com>
 * @date        2011-02-21
 * @link		http://www.amiaoagou.com
 */
class benchmark {

    var $marker = array();

    /**
     * Set a benchmark marker
     *
     * 多次调用这个函数可以使几个
     * 可定时执行点
     *
     * @access	public
     * @param	string	$name	name of the marker
     * @return	void
     */
    function mark($name)
    {
        $this->marker[$name] = microtime();
    }

    // --------------------------------------------------------------------

    /**
     * 计算两个标记点之间的时间差
     *
     * 如果第一个参数是空的，这个功能，而不是返回
     * {ELAPSED_TIME}伪变量。这使全系统
     * 执行时间将显示在模板中。输出类
     * 交换此变量的真正价值。
     *
     * @access	public
     * @param	string	a particular marked point
     * @param	string	a particular marked point
     * @param	integer	the number of decimal places
     * @return	mixed
     */
    function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
    {
        if ($point1 == '')
        {
            return '{elapsed_time}';
        }

        if ( ! isset($this->marker[$point1]))
        {
            return '';
        }

        if ( ! isset($this->marker[$point2]))
        {
            $this->marker[$point2] = microtime();
        }

        list($sm, $ss) = explode(' ', $this->marker[$point1]);
        list($em, $es) = explode(' ', $this->marker[$point2]);

        return number_format(($em + $es) - ($sm + $ss), $decimals);
    }

    // --------------------------------------------------------------------

    /**
     * Memory Usage
     *
     * This function returns the {memory_usage} pseudo-variable.
     * This permits it to be put it anywhere in a template
     * without the memory being calculated until the end.
     * The output class will swap the real value for this variable.
     *
     * @access	public
     * @return	string
     */
    function memory_usage()
    {
        return '{memory_usage}';
    }

}

// END benchmark class

/* End of file benchmark.class.php */
/* Location: ./system/core/benchmark.class.php */